package com.qf.controller;

import com.qf.entity.User;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletInputStream;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.Scanner;

@RestController
@RequestMapping("/server1")
public class Server1Controller {

    @Value("${server.port}")
    private String port;

    @RequestMapping("/hello")
    public String hello(@RequestBody User user,HttpServletResponse response,HttpServletRequest request) throws IOException {

//        BufferedReader reader = request.getReader();
//        StringBuffer buffer = new StringBuffer();
//        while(reader.readLine() != null){
//        }

        // 1.创建一个cookie

        Cookie cookie = new Cookie("server-1", "server-1-value");

        // 2.设置cookie的属性
        cookie.setMaxAge(60 * 60 * 24 * 7);
        cookie.setHttpOnly(false); // 可以通过js访问cookie
        cookie.setSecure(false); // 走http的协议
//        cookie.setValue(); // 覆盖的上面的value值
        cookie.setPath("/"); // 不设置，默认是当前路径

        // 3.把cookie写入浏览器中
        response.addCookie(cookie);

        return "hello:" + port;
    }
}
